home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Edit List and Spin / NotepadCloneWithRegistry / NotepadCloneWithRegistry.cs next >
Encoding:
Text File  |  2001-01-15  |  3.6 KB  |  108 lines

  1. //-------------------------------------------------------
  2. // NotepadCloneWithRegistry.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------------------------
  4. using Microsoft.Win32;
  5. using System;
  6. using System.Drawing;
  7. using System.Windows.Forms;
  8.  
  9. class NotepadCloneWithRegistry: NotepadCloneNoMenu
  10. {
  11.      Rectangle        rectNormal;
  12.      protected string strProgName;
  13.      string           strRegKey    = "Software\\ProgrammingWindowsWithCSharp\\";
  14.      const string     strWinState  = "WindowState";
  15.      const string     strLocationX = "LocationX";
  16.      const string     strLocationY = "LocationY";
  17.      const string     strWidth     = "Width";
  18.      const string     strHeight    = "Height";
  19.  
  20.      public new static void Main()
  21.      {
  22.           Application.Run(new NotepadCloneWithRegistry());
  23.      }
  24.      public NotepadCloneWithRegistry()
  25.      {
  26.           Text = strProgName = "Notepad Clone with Registry";
  27.           rectNormal = DesktopBounds;
  28.      }
  29.      protected override void OnMove(EventArgs ea)
  30.      {
  31.           base.OnMove(ea);
  32.  
  33.           if (WindowState == FormWindowState.Normal)
  34.                rectNormal = DesktopBounds;
  35.      }
  36.      protected override void OnResize(EventArgs ea)
  37.      {
  38.           base.OnResize(ea);
  39.  
  40.           if (WindowState == FormWindowState.Normal)
  41.                rectNormal = DesktopBounds;
  42.      }
  43.      protected override void OnLoad(EventArgs ea)
  44.      {
  45.           base.OnLoad(ea);
  46.  
  47.                // Construct complete registry key.
  48.  
  49.           strRegKey = strRegKey + strProgName;
  50.  
  51.                // Load registry information.
  52.  
  53.           RegistryKey regkey = Registry.CurrentUser.OpenSubKey(strRegKey);
  54.  
  55.           if (regkey != null)
  56.           {
  57.                LoadRegistryInfo(regkey);
  58.                regkey.Close();
  59.           }
  60.      }
  61.      protected override void OnClosed(EventArgs ea)
  62.      {
  63.           base.OnClosed(ea);
  64.  
  65.                // Save registry information.
  66.  
  67.           RegistryKey regkey = 
  68.                          Registry.CurrentUser.OpenSubKey(strRegKey, true);
  69.  
  70.           if (regkey == null)
  71.                regkey = Registry.CurrentUser.CreateSubKey(strRegKey);
  72.  
  73.           SaveRegistryInfo(regkey);
  74.           regkey.Close();
  75.      }
  76.      protected virtual void SaveRegistryInfo(RegistryKey regkey)
  77.      {
  78.           regkey.SetValue(strWinState,  (int) WindowState);
  79.           regkey.SetValue(strLocationX, rectNormal.X);
  80.           regkey.SetValue(strLocationY, rectNormal.Y);
  81.           regkey.SetValue(strWidth,     rectNormal.Width);
  82.           regkey.SetValue(strHeight,    rectNormal.Height);
  83.      }
  84.      protected virtual void LoadRegistryInfo(RegistryKey regkey)
  85.      {
  86.           int x  = (int) regkey.GetValue(strLocationX, 100);
  87.           int y  = (int) regkey.GetValue(strLocationY, 100);
  88.           int cx = (int) regkey.GetValue(strWidth, 300);
  89.           int cy = (int) regkey.GetValue(strHeight, 300);
  90.  
  91.           rectNormal = new Rectangle(x, y, cx, cy);
  92.  
  93.                // Adjust rectangle for any change in desktop size.
  94.           
  95.           Rectangle rectDesk = SystemInformation.WorkingArea;
  96.  
  97.           rectNormal.Width  = Math.Min(rectNormal.Width,  rectDesk.Width);
  98.           rectNormal.Height = Math.Min(rectNormal.Height, rectDesk.Height);
  99.           rectNormal.X -= Math.Max(rectNormal.Right  - rectDesk.Right,  0);
  100.           rectNormal.Y -= Math.Max(rectNormal.Bottom - rectDesk.Bottom, 0);
  101.  
  102.                // Set form properties.
  103.  
  104.           DesktopBounds = rectNormal;
  105.           WindowState = (FormWindowState) regkey.GetValue(strWinState, 0);
  106.      }
  107. }
  108.